#include "core.h"
#include "xLevelMgr.h"
#include "JsonEncoding.h"

//-------------------------------------------------------------------------------------------------------------
IMP_SINGLETON_CLASS(CLevelMgr)

CLevelMgr::CLevelMgr()
{

}

CLevelMgr::~CLevelMgr()
{
	LevelList::iterator it = m_mapLevels.begin();
	for( ; it!= m_mapLevels.end(); ++it )
	{
		delete it->second;
	}
	m_mapLevels.clear();
}

BOOL GetInt(rapidxml::xml_node<> * xml, const char* key, S32& value)
{
	rapidxml::xml_attribute<char> * attr = xml->first_attribute(key);
	if (attr)
	{
		value = atol(attr->value());
		return TRUE;
	}
	return FALSE;
}

BOOL GetUint(rapidxml::xml_node<> * xml, const char* key, U32& value)
{
	rapidxml::xml_attribute<char> * attr = xml->first_attribute(key);
	if (attr)
	{
		value = (U32)atol(attr->value());
		return TRUE;
	}
	return FALSE;
}

BOOL GetString(rapidxml::xml_node<> * xml, const char* key, std::string& value)
{
	rapidxml::xml_attribute<char> * attr = xml->first_attribute(key);
	if (attr)
	{
		value = attr->value();
		return TRUE;
	}
	return FALSE;
}

void CLevelMgr::Init(std::string& strXMLFile)
{
	//<node chap_list="" enter_condition="[1]" level_id="1" level_type="1" main_type="1" open_condition="[1]" sence_id="1" through_reward="[0,0]"/>
	rapidxml::file<> fdoc( strXMLFile.c_str() );
	rapidxml::xml_document<> doc;    
	doc.parse< 0 >( fdoc.data() ); 
	rapidxml::xml_node<> * node_root = doc.first_node( "level" );

	std::string strValue;
	Json::Value root;

	rapidxml::xml_node<> * node_media = node_root->first_node( "node" );
	while ( node_media )
	{
		LevelAttr* pLevel = new LevelAttr;

		GetUint(node_media, "level_id", pLevel->nLevelID);
		GetUint(node_media, "sence_id", pLevel->nSenceID);
		GetUint(node_media, "main_type", (U32&)pLevel->eLevelMainType);
		GetUint(node_media, "level_type", (U32&)pLevel->eLevelType);

		if(GetString(node_media, "through_reward", strValue))
		{
			if (CJsonEncoding::loadStr(strValue, root))
			{
				UINT array_size = root.size();
				if (array_size == 2)
				{
				//	pLevel->tNpcList.npc_count = root[0u].asInt();
				//	pLevel->tNpcList.boss_count = root[1u].asInt();
				}
			}
		}

		std::pair<LevelList::iterator, bool> ins = 
			m_mapLevels.insert(LevelList::value_type(pLevel->nLevelID, pLevel));
		if( !ins.second )
		{
			delete pLevel;
		}

		node_media = node_media->next_sibling();
	}
}

LevelAttr* CLevelMgr::getLevelTypeById(U32 nLevelId)
{
	LevelList::iterator it = m_mapLevels.find(nLevelId);
	if (it != m_mapLevels.end())
	{
		return it->second;
	}
	return NULL;
}

void CLevelMgr::CreateRoom()
{
	LevelList::iterator it = m_mapLevels.begin();
	for( ; it!= m_mapLevels.end(); ++it )
	{
		LevelAttr* la = it->second;
		if (la->eLevelMainType == LMT_CITY)
		{
			UINT nLeveID = la->nLevelID;
			UINT nSenceID = la->nSenceID;

	/*		std::vector<UINT> vecGuids;
			CLineMgr::getInstance()->getCurInitLineGuids(nLeveID, vecGuids);
			for (std::vector<UINT>::iterator i = vecGuids.begin(); i != vecGuids.end(); ++i)
			{
				UINT guid = *i;
				UINT line_id = guid & 0xFF;

				Co::CEntity * room = NULL;
				NEW_ROOM( room, Sbase::ERoomDef::ROOM_WORLD );

				room->getAttr(EAttr::LINE_ID)->setUINT(line_id);

				Sbase::CComRoom *com_room = 
					room->getCom< Sbase::CComRoom >( Sbase::ECom::COM_ROOM );
				com_room->create( Sbase::ERoomDef::ROOM_ID_LEVEL_BEGIN + nLeveID, guid );

				Sbase::CComMapMgr *com_map_mgr =
					room->getCom< Sbase::CComMapMgr >( Sbase::ECom::COM_MAP_MGR );
				Sbase::CMapMgr * map_mgr = com_map_mgr->getMapMgr();
				map_mgr->addMap( nSenceID );

				Sbase::CRoomMgr::getInstance()->addRoom( room );
				entity_mgr->addEntity( room );

				map_mgr->getMapById( nSenceID )->getMapNpc()->flashNpcAll( room );

				com_map_mgr->setLevelID(nLeveID);
				com_map_mgr->setLevelType(it->second->eLevelType);
				CLineMgr::getInstance()->updateLineInfo(nLeveID, line_id, 0);
			}*/
		}
	}
}